/*
 * Filename:    CostCoverController.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014年9月29日
 */
package com.paic.mhis.hcpms.quota.settlement.web.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.paic.mhis.hcpms.quota.settlement.biz.service.CostCoverService;
import com.paic.mhis.hcpms.quota.settlement.dto.CostCodeLabelDTO;
import com.paic.mhis.hcpms.quota.settlement.dto.CostCoverDTO;
import com.paic.mhis.hcpms.quota.settlement.dto.CostDataDTO;
import com.paic.mhis.hcpms.quota.settlement.dto.CostParamterDTO;
import com.paic.mhis.hcpms.quota.settlement.web.vo.CostCoverVO;
import com.paic.mhis.hcpms.quota.settlement.web.vo.CostDataVO;
import com.paic.pafa.appclient.annotation.BizServicesProxy;

@Controller
@RequestMapping("/cost/")
public class CostCoverController {

	@BizServicesProxy(beanName = "costCoverService")
	private CostCoverService costCoverService;

	@RequestMapping("getCostCover.do")
	@ResponseBody
	public Map<String, Object> getCostCover(@ModelAttribute CostParamterDTO vo, HttpServletRequest request) {

		String hospitalLev = vo.getHospitalLev();
		String hospitalType = vo.getHospitalType();
		String visitType = vo.getVisitType();
		Map<String, Object> result = new HashMap<String, Object>();
		CostDataDTO bean = this.costCoverService.queryCostDataDTOByParameter(hospitalLev, hospitalType, visitType,
				vo.getDate());
		if (bean != null) {
			CostDataVO cdv = this.getCostDataDTOtoObject(bean);
			result.put("data", cdv);
		} else {
			result.put("data", null);
		}

		return result;
	}

	/**
	 * 将DTO 转换成VO
	 * 
	 * @param bean
	 * @return
	 */
	private CostDataVO getCostDataDTOtoObject(CostDataDTO bean) {
		CostDataVO returnVo = new CostDataVO();

		List<CostCoverVO> costCoverVoList = new ArrayList<CostCoverVO>();

		List<CostCoverDTO> costList = bean.getCostList();

		for (CostCoverDTO dto : costList) {
			CostCoverVO voBean = new CostCoverVO();
			BigDecimal medical = dto.getMedicalPer();
			BigDecimal opertion = dto.getOperationPer();
			BigDecimal check = dto.getCheckPer();
			BigDecimal other = dto.getOtherPer();

			String date = dto.getDate();
			// 都是为0的情况下，显示-
			if (null == medical && null == opertion && null == check) {
				voBean.setMedicalPer("-");
				voBean.setOperationPer("-");
				voBean.setCheckPer("-");
				voBean.setOtherPer("-");
			} else {
				voBean.setMedicalPer(this.getStringPerValue(medical));
				voBean.setOperationPer(this.getStringPerValue(opertion));
				voBean.setCheckPer(this.getStringPerValue(check));
				voBean.setOtherPer(this.getStringPerValue(other));

			}
			voBean.setDate(date);

			costCoverVoList.add(voBean);

		}

//		List<String> voDateList = new ArrayList<String>();
//		for (String str : bean.getDate()) {
//			String currentValue = str.substring(2);
//			voDateList.add(currentValue);
//		}
//
//		returnVo.setDate(voDateList.toArray(new String[0]));
		
		returnVo.setCostList(costCoverVoList);

		BigDecimal[] medical = bean.getMedicalPer();
		BigDecimal[] opertion = bean.getOperationPer();
		BigDecimal[] check = bean.getCheckPer();
		BigDecimal[] other = bean.getOtherPer();

		String[] medicals = new String[medical.length];
		String[] opertions = new String[opertion.length];
		String[] checks = new String[check.length];
		String[] others = new String[other.length];
		for (int i = 0; i < medical.length; i++) {
			BigDecimal temp = medical[i].setScale(2, BigDecimal.ROUND_HALF_UP);
			medicals[i] = temp.toString();
		}
		for (int i = 0; i < opertion.length; i++) {
			BigDecimal temp = opertion[i].setScale(2, BigDecimal.ROUND_HALF_UP);
			opertions[i] = temp.toString();
		}
		for (int i = 0; i < check.length; i++) {
			BigDecimal temp = check[i].setScale(2, BigDecimal.ROUND_HALF_UP);
			checks[i] = temp.toString();
		}
		for (int i = 0; i < other.length; i++) {
			BigDecimal temp = other[i].setScale(2, BigDecimal.ROUND_HALF_UP);
			if ("100.00".equals(temp.toString())) {
				others[i] = "-";
				medicals[i] = "-";
				opertions[i] = "-";
				checks[i] = "-";
			} else {
				others[i] = temp.toString();
			}

		}

		returnVo.setMedicalPer(medicals);
		returnVo.setOperationPer(opertions);
		returnVo.setCheckPer(checks);
		returnVo.setOtherPer(others);

		return returnVo;
	}

	/**
	 * 将数值转换成百分比
	 * 
	 * @param value
	 * @return
	 */
	private String getStringPerValue(BigDecimal value) {
		if (value == null) {
			return "0.00%";
		} else {
			BigDecimal currentValue = value.setScale(2, BigDecimal.ROUND_HALF_UP);
			;
			return currentValue + "%";
		}

	}

	/**
	 * 将日期格式2013-02转换成2013年02月
	 * 
	 * @param date
	 * @return
	 */
	private String dateStrToString(String date) {
		Date value = this.strToDate(date);
		String str = this.dateToStr(value);

		return str;
	}

	public String dateToStr(Date date) {

		SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月");
		String str = format.format(date);
		return str;
	}

	/**
	 * 字符串转换成日期
	 * 
	 * @param str
	 * @return date
	 */
	public Date strToDate(String str) {

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
		Date date = null;
		try {
			date = format.parse(str);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}

	@RequestMapping("getCostCoverCodeLabel.do")
	@ResponseBody
	public Map<String, Object> getCodeLabel() {

		Map<String, Object> paramMap = new HashMap<String, Object>();

		List<CostCodeLabelDTO> list = this.costCoverService.queryCostCodeLabelVO();

		paramMap.put("data", list);

		return paramMap;

	}

}
